package com.beordie.dao.impl;

import com.beordie.dao.IMenuDao;
import com.beordie.model.Menu;
import com.beordie.model.Role;
import com.beordie.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname MenuDaoImpl
 * @Description TODO
 * @Date 2021/6/13 22:20
 * @Created 30500
 */
public class MenuDaoImpl extends DBUtils implements IMenuDao {
    @Override
    public int getAllTotal() {
        // 查询语句
        String sql = "select count(*) from menu";

        // 查询数据库信息
        resultSet = super.query(sql, null);
        int result = 0;
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充信息
                    result = resultSet.getInt(1);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                super.close();
            }
        }
        return result;
    }

    @Override
    public List<Menu> getByLimit(int index, int pageSize) {
        // 查询语句
        String sql = "select * from menu limit ?,?";
        List<Integer> params = new ArrayList<>();
        params.add((index - 1) * pageSize);
        params.add(pageSize);

        // 查询数据库信息
        resultSet = super.query(sql, params);
        List<Menu> menus = new ArrayList<>();
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充角色信息
                    Menu menu = new Menu();
                    menu.setMenuId(resultSet.getInt("menuid"));
                    menu.setMenuName(resultSet.getString("menuname"));
                    menu.setUpMenuId(resultSet.getInt("upmenuid"));
                    menu.setState(resultSet.getInt("state"));
                    menu.setDesc(resultSet.getString("desc"));
                    menu.setUrl(resultSet.getString("url"));
                    menus.add(menu);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                super.close();
            }
        }
        return menus;
    }

    @Override
    public List<Menu> getAll() {
        // 查询语句
        String sql = "select * from menu";

        // 查询数据库信息
        resultSet = super.query(sql, null);
        List<Menu> menus = new ArrayList<>();
        if (resultSet != null){
            try {
                while (resultSet.next()) {
                    // 填充角色信息
                    Menu menu = new Menu();
                    menu.setMenuId(resultSet.getInt("menuid"));
                    menu.setMenuName(resultSet.getString("menuname"));
                    menu.setUpMenuId(resultSet.getInt("upmenuid"));
                    menu.setState(resultSet.getInt("state"));
                    menu.setDesc(resultSet.getString("desc"));
                    menu.setUrl(resultSet.getString("url"));
                    menus.add(menu);
                }
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            } finally {
                super.close();
            }
        }
        return menus;
    }
}
